package com.dx.mes.service.plan.service;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.bean.erp.ERSVXFR;
import com.dx.mes.common.core.rabbitmq.BaseConnFactory;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.plan.service.IFdpaplyrsService;
import com.dx.mes.facade.plan.tx.Fdpaplyrs.FdpaplyrsI;
import com.dx.mes.facade.plan.tx.Fdpaplyrs.FdpaplyrsIA;
import com.dx.mes.facade.plan.tx.Fdpaplyrs.FdpaplyrsO;
import com.dx.mes.facade.plan.tx.Fdpaplyrs.FdpaplyrsOA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.dx.mes.common.bean.def.GenericDef.*;

/**
 * Created by admin on 2018/3/16.
 */
@Service("fdpaplyrsService")
public class FdpaplyrsService implements IFdpaplyrsService {

    private static final String TX_NAME = T_FDPAPLYRS;
    private LogUtils logUtils;

    @Autowired
    private IRetWhRsvDao retWhRsvDao;
    @Autowired
    private IBisMtrlDao bisMtrlDao;
    @Autowired
    private IRetWhMtrlDao retWhMtrlDao;
    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;
    @Autowired
    private IRetWODao   retWODao;
    @Autowired
    private IRetBoxDao  retBoxDao;
    @Autowired
    private IRetPrdAbnDao  retPrdAbnDao;
    @Resource
    private BaseConnFactory baseConnFactory;

    @Override
    public String subMainProc(String evt_no, String strInTrx) {
        AppContext.clear();
        AppContext.setCurrServiceName(FdpaplyrsService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FdpaplyrsService.class);
        logUtils.info("[InTrx:" + strInTrx + "]");
        String strOutTrx = "";
        FdpaplyrsI inTrx = new FdpaplyrsI();
        FdpaplyrsO outTrx = new FdpaplyrsO();
        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInTrx, FdpaplyrsI.class);
            char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
                case ACTION_FLG_QUERY:
                    queryFnc(inTrx,outTrx);
                    break;
                case ACTION_FLG_ADD:
                    addFnc(inTrx,outTrx);
                    break;
                case 'R':
                    findRsvNoFnc(inTrx,outTrx);
                    break;
                case 'S':
                    saveFnc(inTrx,outTrx);
                    break;
                default:
                    errActionFnc(outTrx);
            }
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                retWhRsvDao.flushCurSession();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg(ex.toString());
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            strOutTrx = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + strOutTrx + "]");
        }
        return strOutTrx;
    }
    public void queryFnc(FdpaplyrsI inTrx,FdpaplyrsO outTrx){

        String rsv_no = inTrx.getRsv_no();
        String type  = inTrx.getType_id();
        String hql="";
        if("I".equals(type)){
            hql="From Ret_wh_rsv where rsv_no='" + rsv_no + "' and evt_cate='SRSI'";
        }else if("O".equals(type)){
            hql="From Ret_wh_rsv where evt_cate='" + EVT_SAP_RSV_DELETE + "' ";
            if(!Util.isSpaceCheck(rsv_no)){
                hql= hql+"and rsv_no='" + rsv_no + "'";
            }
        }
        List<Ret_wh_rsv>  list = retWhRsvDao.find(hql);
        List<FdpaplyrsOA>  orayList = new ArrayList<>();
        for(Ret_wh_rsv ret_wh_rsv :list){
            FdpaplyrsOA oray = new FdpaplyrsOA();
            oray.setRsv_no(Integer.valueOf(ret_wh_rsv.getRsv_no()));
            oray.setItem_no(Integer.valueOf(ret_wh_rsv.getItem_no()));
            oray.setMtrl_prod_id(ret_wh_rsv.getMtrl_prod_id_fk());
            oray.setMtrl_pln_qty(ret_wh_rsv.getMtrl_pln_qty());
            //oray.setEvt_date();
            oray.setMtrl_act_qty(String.valueOf(ret_wh_rsv.getMtrl_act_qty()));
            oray.setMtrl_unit(ret_wh_rsv.getMtrl_unit());
            oray.setMtrl_lot_id(ret_wh_rsv.getMtrl_lot_id());
            oray.setMove_typ(ret_wh_rsv.getMove_typ());
            oray.setFab_id(ret_wh_rsv.getFab_id_fk());
            oray.setSource_shop(ret_wh_rsv.getSource_shop());
            oray.setDest_shop(ret_wh_rsv.getDest_shop());
            orayList.add(oray);
        }
        outTrx.setOary(orayList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void addFnc(FdpaplyrsI inTrx,FdpaplyrsO outTrx){

        List<FdpaplyrsIA> iaryLsit = inTrx.getIary();
        List<Ret_wh_rsv> inList = new ArrayList<>();//入库调拨
        List<Ret_wh_rsv> outList = new ArrayList<>();//退库调拨
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        for(FdpaplyrsIA iary : iaryLsit){
            Ret_wh_rsv ret_wh_rsv =null;
            if("Z03".equals(iary.getMove_typ())){
                ret_wh_rsv = new Ret_wh_rsv();
                ret_wh_rsv.setMtrl_seq_id(GUIDGenerator.javaGUID());
                ret_wh_rsv.setRsv_no(iary.getRsv_no().toString());
                ret_wh_rsv.setItem_no(iary.getItem_no().toString());
                ret_wh_rsv.setMtrl_prod_id_fk(iary.getMtrl_prod_id());
                ret_wh_rsv.setMtrl_pln_qty(iary.getMtrl_pln_qty());
                Bis_mtrl bis_mtrl = bisMtrlDao.get(iary.getMtrl_prod_id());
                if(bis_mtrl!=null){
                    ret_wh_rsv.setMtrl_cate(bis_mtrl.getMtrl_cate());
                }
                //没有evt_date
                ret_wh_rsv.setMtrl_act_qty(Double.valueOf(iary.getMtrl_act_qty()));
                ret_wh_rsv.setMtrl_unit(iary.getMtrl_unit());
                ret_wh_rsv.setMtrl_lot_id(iary.getMtrl_lot_id());
                ret_wh_rsv.setMove_typ(iary.getMove_typ());
                ret_wh_rsv.setFab_id_fk(iary.getFab_id());
                ret_wh_rsv.setSource_shop(iary.getSource_shop());
                ret_wh_rsv.setDest_shop(iary.getDest_shop());
                ret_wh_rsv.setEvt_cate(EVT_SAP_RSV_IN);
                ret_wh_rsv.setEvt_timestamp(current_timestamp);
                ret_wh_rsv.setEvt_usr("SAP");
                inList.add(ret_wh_rsv);
            }else if("Z04".equals(iary.getMove_typ())){//Z04
                String hql ="from Ret_wh_rsv where mtrl_prod_id_fk='" + iary.getMtrl_prod_id()
                        + "' and  mtrl_lot_id ='" + iary.getMtrl_lot_id() + "' and  move_typ ='" + iary.getMove_typ() + "' and  rsv_no ='" + iary.getMes_rsv_no() + "'";
                List<Ret_wh_rsv> whRsvList = retWhRsvDao.find(hql);
                if(whRsvList!=null && whRsvList.size()>0){
                    ret_wh_rsv = whRsvList.get(0);
                    ret_wh_rsv.setMtrl_act_qty(ret_wh_rsv.getMtrl_act_qty()+Double.valueOf(iary.getMtrl_act_qty()));
                    ret_wh_rsv.setEvt_cate(EVT_MES_RSV_DELETE);
                    ret_wh_rsv.setEvt_timestamp(current_timestamp);
                    ret_wh_rsv.setEvt_usr("SAP");
                    outList.add(ret_wh_rsv);
                }
            }else if("Z05".equals(iary.getMove_typ())){//入库、报废、半成品过账
                char type = iary.getMes_rsv_no().charAt(0);//S:报废确认  W:成品入库确认  H:半成品入库确认
                switch (type) {
                    case 'S':
                        sureScrap(inTrx,outTrx);
                        break;
                    case 'W':
                        sureWholeWhIn(inTrx,outTrx);
                        break;
                    case 'H':
                        sureHalfWhIn(inTrx,outTrx);
                        break;
                }
            }
        }
        if(inList.size()>0){//接受预留调拨
            retWhRsvDao.saveOrUpdateAll(inList);
        }
        if(outList.size()>0){//确认调拨后，回传信息更新
            retWhRsvDao.saveOrUpdateAll(outList);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void saveFnc(FdpaplyrsI inTrx,FdpaplyrsO outTrx) {
        String rsv_no = inTrx.getRsv_no();
        String typeId = inTrx.getType_id();
        String hql = "From Ret_wh_rsv where rsv_no='" + rsv_no + "'";
        List<Ret_wh_rsv> list = retWhRsvDao.find(hql);
        Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
        //向SAP发送一个调拨为提货量,写入库存表后，后删除临时表数据
        List<Ret_wh_rsv> sureList = new ArrayList<Ret_wh_rsv>();
        List<Ret_wh_mtrl> mtrlList = new ArrayList<Ret_wh_mtrl>();
        List<ERSVXFR> ersvxfrList = new ArrayList<ERSVXFR>();
        for(Ret_wh_rsv ret_wh_rsv:list){
        	if(!EVT_SAP_RSV_IN.equals(ret_wh_rsv.getEvt_cate())){
                outTrx.setRtn_mesg("预留号[" + inTrx.getRsv_no() + "]已经过账,请勿重复过账");
                outTrx.setRtn_code(E_FDPAPLYRS_RSV_STARUS_ERROR + "");
                return ;
        	}
             //组ESB返回信息
        	ERSVXFR ersvxfr = new ERSVXFR();
        	ersvxfr.setRSNUM(ret_wh_rsv.getRsv_no());
        	ersvxfr.setRSPOS(ret_wh_rsv.getItem_no());
            Format dateFormat = new SimpleDateFormat("yyyyMMdd");
            String date  = dateFormat.format(new Date());
            ersvxfr.setBDTER(date);
            ersvxfr.setBWART("Z03");
            ersvxfr.setWEMPF("");
            ersvxfr.setWERKS(ret_wh_rsv.getFab_id_fk());
            ersvxfr.setMATNR(ret_wh_rsv.getMtrl_prod_id_fk());
            if("S".equals(typeId)){
            	ersvxfr.setBDMNG(ret_wh_rsv.getMtrl_pln_qty());
            }else {
            	ersvxfr.setBDMNG(0.0);
            }
            ersvxfr.setCHARG(ret_wh_rsv.getMtrl_lot_id());
            ersvxfr.setLGORT(ret_wh_rsv.getSource_shop());
            ersvxfr.setUMLGO(ret_wh_rsv.getDest_shop());
            ersvxfrList.add(ersvxfr);

            if("S".equals(typeId)){
                Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_wh_rsv.getMtrl_prod_id_fk());
                if(bis_mtrl == null){
                    outTrx.setRtn_mesg("未找到物料代码[" + ret_wh_rsv.getMtrl_prod_id_fk() + "]的基础信息[BIS_MTRL]");
                    outTrx.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND+"");
                    return ;
                }
                String hqlM ="FROM Ret_wh_mtrl WHERE dest_shop='" + ret_wh_rsv.getDest_shop()
                        + "' AND mtrl_prod_id_fk ='" + ret_wh_rsv.getMtrl_prod_id_fk() + "'";
                if(!Util.isSpaceCheck(ret_wh_rsv.getMtrl_lot_id())){
                    hqlM = hqlM + " AND mtrl_lot_id ='" + ret_wh_rsv.getMtrl_lot_id() + "'";
                }else{
                    hqlM = hqlM + " AND mtrl_lot_id is null";
                }
                Ret_wh_mtrl ret_wh_mtrl =null;
                List<Ret_wh_mtrl>  ret_wh_mtrls = retWhMtrlDao.find(hqlM);
                if(ret_wh_mtrls!=null  && ret_wh_mtrls.size()>0){
                    ret_wh_mtrl  = ret_wh_mtrls.get(0);
                    ret_wh_mtrl.setMtrl_qty(ret_wh_mtrl.getMtrl_qty()+ret_wh_rsv.getMtrl_pln_qty());
                    ret_wh_mtrl.setEvt_cate(EVT_TOOL_SPARE_WH);
                    ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
                    ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
                }else {
                    ret_wh_mtrl = new Ret_wh_mtrl();
                    ret_wh_mtrl.setMtrl_seq_id(GUIDGenerator.javaGUID());
                    ret_wh_mtrl.setMtrl_prod_id_fk(ret_wh_rsv.getMtrl_prod_id_fk());
                    ret_wh_mtrl.setMtrl_lot_id(ret_wh_rsv.getMtrl_lot_id());
                    ret_wh_mtrl.setMtrl_cate(ret_wh_rsv.getMtrl_cate());
                    ret_wh_mtrl.setMtrl_cus_id_fk(bis_mtrl.getMtrl_mkr());
                    ret_wh_mtrl.setInwh_timestamp(gEvtTimestamp);
                    ret_wh_mtrl.setMtrl_qty(ret_wh_rsv.getMtrl_pln_qty());
                    ret_wh_mtrl.setEvt_qty(Double.valueOf(0));
                    ret_wh_mtrl.setDest_shop(ret_wh_rsv.getDest_shop());
                    ret_wh_mtrl.setEvt_cate(EVT_TOOL_SPARE_WH);
                    ret_wh_mtrl.setEvt_timestamp(gEvtTimestamp);
                    ret_wh_mtrl.setEvt_usr(inTrx.getEvt_usr());
                }
                mtrlList.add(ret_wh_mtrl);
            }
             //更新调拨临时表信息
            ret_wh_rsv.setMtrl_act_qty(ret_wh_rsv.getMtrl_pln_qty());
            ret_wh_rsv.setEvt_usr(inTrx.getEvt_usr());
            ret_wh_rsv.setEvt_cate(EVT_SAP_RSV_DELETE);
            ret_wh_rsv.setEvt_timestamp(gEvtTimestamp);
            sureList.add(ret_wh_rsv);
        }
    /*    try {
            baseConnFactory.addSAPREQProc("ERSVXFR", JacksonUtil.toJSONStr(ersvxfrList));
        }catch (Exception e){
            e.printStackTrace();
        }*/
        if("S".equals(typeId)){
            retWhMtrlDao.saveOrUpdateAll(mtrlList);
        }
        if(sureList.size()>0){
            retWhRsvDao.saveOrUpdateAll(sureList);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
    public void findRsvNoFnc(FdpaplyrsI inTrx,FdpaplyrsO outTrx){

        String type  = inTrx.getType_id();
        String hql="";
        if("I".equals(type)){
             hql="From Ret_wh_rsv where evt_cate='" + EVT_SAP_RSV_IN + "' ";
        }else if("O".equals(type)){
            hql="From Ret_wh_rsv where evt_cate='" + EVT_SAP_RSV_DELETE + "' ";
        }
        List<Ret_wh_rsv>  list = retWhRsvDao.find(hql);
        Map<String,Ret_wh_rsv> map = new HashMap<>();
        for(Ret_wh_rsv ret_wh_rsv :list){
           map.put(ret_wh_rsv.getRsv_no(),ret_wh_rsv);
        }
        List<FdpaplyrsOA>  orayList = new ArrayList<>();
        for(Map.Entry<String,Ret_wh_rsv> entry : map.entrySet()){
            FdpaplyrsOA oray = new FdpaplyrsOA();
            oray.setRsv_no(Integer.valueOf(entry.getValue().getRsv_no()));
            oray.setItem_no(Integer.valueOf(entry.getValue().getItem_no()));
            oray.setMtrl_prod_id(entry.getValue().getMtrl_prod_id_fk());
            oray.setMtrl_pln_qty(entry.getValue().getMtrl_pln_qty());
            //oray.setEvt_date();
            oray.setMtrl_act_qty(String.valueOf(entry.getValue().getMtrl_act_qty()));
            oray.setMtrl_unit(entry.getValue().getMtrl_unit());
            oray.setMtrl_lot_id(entry.getValue().getMtrl_lot_id());
            oray.setMove_typ(entry.getValue().getMove_typ());
            oray.setFab_id(entry.getValue().getFab_id_fk());
            oray.setSource_shop(entry.getValue().getSource_shop());
            oray.setDest_shop(entry.getValue().getDest_shop());
            orayList.add(oray);
        }
        outTrx.setOary(orayList);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    public void sureScrap(FdpaplyrsI inTrx,FdpaplyrsO outTrx) {
        List<FdpaplyrsIA> iaryList = inTrx.getIary();
        for(FdpaplyrsIA iary : iaryList){
            //确认报废，将屏体制程信息清空写入abn，然后工单报废数量加
            String  mesRsvNo = iary.getMes_rsv_no();
            String hql= "from Ret_prd_info where rsv_no='" + mesRsvNo + "' ";
            List<Ret_prd_info> prdInfoList = retPrdInfoDao.find(hql);
            Timestamp gEvtTimestamp = Util.getCurrentTimestamp();

            List<Ret_prd_info> prdInfoNew  = new ArrayList<>();
            if(prdInfoList!=null && prdInfoList.size()>0){
                for(Ret_prd_info ret_prd_info :prdInfoList){
                    //写入abn 表
                    Ret_prd_abn newScrp = new Ret_prd_abn();
                    Ret_prd_abnId id = new Ret_prd_abnId();
                    String evt_seq_id = GUIDGenerator.javaGUID();
                    id.setEvt_seq_id_fk(evt_seq_id);
                    id.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());
                    newScrp.setId(id);
                    newScrp.setPrd_stat(PROC_SCRP);
                    newScrp.setPv_prd_stat(ret_prd_info.getPrd_stat());
                    newScrp.setWo_id_fk(ret_prd_info.getWo_id_fk());

                    newScrp.setCr_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
                    newScrp.setCr_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
                    newScrp.setCr_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
                    newScrp.setCr_path_id_fk(ret_prd_info.getCr_path_id_fk());
                    newScrp.setCr_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
                    newScrp.setCr_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
                    newScrp.setCr_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
                    newScrp.setCr_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
                    newScrp.setCr_tool_run_mode(ret_prd_info.getCr_tool_run_mode());

                    newScrp.setNx_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
                    newScrp.setNx_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
                    newScrp.setNx_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
                    newScrp.setNx_path_id_fk(ret_prd_info.getNx_path_id_fk());
                    newScrp.setNx_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
                    newScrp.setNx_toolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
                    newScrp.setNx_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
                    newScrp.setNx_tool_run_mode(ret_prd_info.getNx_tool_run_mode());
                    newScrp.setAct_code(ABN_ACTION_SCRAP);
                    newScrp.setRsn_cate(ret_prd_info.getRsn_cate());
                    newScrp.setRsn_code(ret_prd_info.getRsn_code());
                    newScrp.setRsn_dept(ret_prd_info.getRsn_dept());
                    newScrp.setEvt_cate(EVT_SAP_MES_SCSURE);
                    newScrp.setEvt_dept(ret_prd_info.getEvt_dept());
                    newScrp.setEvt_usr("SAP");
                    newScrp.setEvt_timestamp(gEvtTimestamp);
                    retPrdAbnDao.save(newScrp);


                    //清空玻璃的制程信息
                    ret_prd_info.setBox_id_fk(_SPACE);
                    ret_prd_info.setSlot_no(_SPACE);
                    ret_prd_info.setPv_prd_stat_fk(ret_prd_info.getPrd_stat());
                    ret_prd_info.setPrd_stat(PROC_SCRP);

                    ret_prd_info.setCr_ope_id_fk(_SPACE);
                    ret_prd_info.setCr_ope_no_fk(_SPACE);
                    ret_prd_info.setCr_ope_ver_fk(_SPACE);
                    ret_prd_info.setCr_path_id_fk(_SPACE);
                    ret_prd_info.setCr_path_ver_fk(_SPACE);
                    ret_prd_info.setCr_proc_id_fk(_SPACE);
                    ret_prd_info.setCr_tool_run_mode(_SPACE);
                    ret_prd_info.setCr_toolg_id_fk(_SPACE);
                    ret_prd_info.setCr_tool_id_fk(_SPACE);
                    ret_prd_info.setTool_id_fk(_SPACE);

                    ret_prd_info.setNx_ope_id_fk(_SPACE);
                    ret_prd_info.setNx_ope_no_fk(_SPACE);
                    ret_prd_info.setNx_ope_ver_fk(_SPACE);
                    ret_prd_info.setNx_path_id_fk(_SPACE);
                    ret_prd_info.setNx_path_ver_fk(_SPACE);
                    ret_prd_info.setNx_proc_id_fk(_SPACE);
                    ret_prd_info.setNx_tool_run_mode(_SPACE);
                    ret_prd_info.setNx_toolg_id_fk(_SPACE);
                    ret_prd_info.setNx_tool_id_fk(_SPACE);

                    ret_prd_info.setDest_shop(iary.getSource_shop());
                    ret_prd_info.setBnk_flg(BANK_COMP);
                    ret_prd_info.setEvt_cate(EVT_SAP_MES_SCSURE);
                    ret_prd_info.setEvt_usr("SAP");
                    ret_prd_info.setEvt_timestamp(gEvtTimestamp);
                    prdInfoNew.add(ret_prd_info);
                }
                retPrdInfoDao.saveOrUpdateAll(prdInfoNew);

                //工单的报废数量增加
                Ret_wo ret_wo = retWODao.get(prdInfoList.get(0).getWo_id_fk());
                if (null != ret_wo) {
                    ret_wo.setScrp_prd_qty(ret_wo.getScrp_prd_qty() + prdInfoList.size());
                    ret_wo.setEvt_usr("SAP");
                    ret_wo.setEvt_timestamp(gEvtTimestamp);
                    retWODao.update(ret_wo);
                }
            }
        }
    }
    public void sureHalfWhIn(FdpaplyrsI inTrx,FdpaplyrsO outTrx) {
        List<FdpaplyrsIA> iaryList = inTrx.getIary();
        for(FdpaplyrsIA iary : iaryList){
            //入库确认后，更新屏体信息，更新内外箱信息，更新工单信息
            String  mesRsvNo = iary.getMes_rsv_no();
            String hql= "from Ret_prd_info where rsv_no='" + mesRsvNo + "' ";
            List<Ret_prd_info> prdInfoList = retPrdInfoDao.find(hql);
            Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
            List<Ret_prd_info> prdInfoNew  = new ArrayList<>();
            if(prdInfoList!=null && prdInfoList.size()>0){
                for(Ret_prd_info ret_prd_info :prdInfoList){
                    ret_prd_info.setBnk_flg(BANK_COMP);
                    ret_prd_info.setDest_shop(iary.getSource_shop());
                    ret_prd_info.setEvt_cate(EVT_SAP_MES_HLSURE);
                    ret_prd_info.setEvt_usr("SAP");
                    ret_prd_info.setEvt_timestamp(gEvtTimestamp);
                    prdInfoNew.add(ret_prd_info);
                }
                retPrdInfoDao.saveOrUpdateAll(prdInfoNew);
                //更新工单
                Ret_wo ret_wo =retWODao.get(prdInfoList.get(0).getWo_id_fk());
                ret_wo.setEvt_timestamp(gEvtTimestamp);
                ret_wo.setEvt_usr("SAP");
                ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty() + prdInfoList.size());
                if (ret_wo.getWh_in_prd_qty().intValue() + ret_wo.getScrp_prd_qty().intValue() > ret_wo.getRl_prd_qty().intValue()) {
                    outTrx.setRtn_code(E_FWPWMSOPE_WO_CNT_NOT_ENOUGH + "");
                    outTrx.setRtn_mesg("工单 [" + prdInfoList.get(0).getWo_id_fk() + "] 解封数量 [" + ret_wo.getRl_prd_qty() + "],报废数量["
                            + ret_wo.getScrp_prd_qty() + "] + 入库数量 [" + ret_wo.getWh_in_prd_qty() + "] 汇总超标！!");
                    return;
                }
                retWODao.update(ret_wo);
            }
        }

    }
    public void sureWholeWhIn(FdpaplyrsI inTrx,FdpaplyrsO outTrx) {
          List<FdpaplyrsIA> iaryList = inTrx.getIary();
          for(FdpaplyrsIA iary : iaryList){
              //入库确认后，更新屏体信息，更新内外箱信息，更新工单信息
              String  mesRsvNo = iary.getMes_rsv_no();
              String hql= "from Ret_prd_info where rsv_no='"+mesRsvNo+"' ";
              List<Ret_prd_info> prdInfoList = retPrdInfoDao.find(hql);
              Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
              List<Ret_prd_info> prdInfoNew  = new ArrayList<>();
              if(prdInfoList!=null && prdInfoList.size()>0){
                 for(Ret_prd_info ret_prd_info :prdInfoList){
                     ret_prd_info.setBnk_flg(BANK_COMP);
                     ret_prd_info.setDest_shop(iary.getSource_shop());
                     ret_prd_info.setEvt_cate(EVT_SAP_MES_WHSURE);
                     ret_prd_info.setEvt_usr("SAP");
                     ret_prd_info.setEvt_timestamp(gEvtTimestamp);
                     prdInfoNew.add(ret_prd_info);
                 }
                 retPrdInfoDao.saveOrUpdateAll(prdInfoNew);

                 //更新外箱信息
                  Ret_box  ret_boxW = retBoxDao.get(prdInfoList.get(0).getShip_box_id_fk());
                  ret_boxW.setBnk_flg(BANK_COMP);
                  ret_boxW.setDest_shop(iary.getSource_shop());
                  ret_boxW.setEvt_cate(EVT_SAP_MES_WHSURE);
                  ret_boxW.setEvt_usr("SAP");
                  ret_boxW.setEvt_timestamp(gEvtTimestamp);
                  retBoxDao.update(ret_boxW);

                  //更新内箱信息
                  Ret_box  ret_boxN = retBoxDao.get(prdInfoList.get(0).getBox_id_fk());
                  ret_boxN.setBnk_flg(BANK_COMP);
                  ret_boxN.setDest_shop(iary.getSource_shop());
                  ret_boxN.setEvt_cate(EVT_SAP_MES_WHSURE);
                  ret_boxN.setEvt_usr("SAP");
                  ret_boxN.setEvt_timestamp(gEvtTimestamp);
                  retBoxDao.update(ret_boxN);

                  //更新工单
                  Ret_wo ret_wo =retWODao.get(prdInfoList.get(0).getWo_id_fk());
                  ret_wo.setEvt_timestamp(gEvtTimestamp);
                  ret_wo.setEvt_usr("SAP");
                  ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty() + prdInfoList.size());
                  if (ret_wo.getWh_in_prd_qty().intValue() + ret_wo.getScrp_prd_qty().intValue() > ret_wo.getRl_prd_qty().intValue()) {
                      outTrx.setRtn_code(E_FDPAPLYRS_WO_CNT_NOT_ENOUGH + "");
                      outTrx.setRtn_mesg("工单 [" + prdInfoList.get(0).getWo_id_fk() + "] 解封数量 [" + ret_wo.getRl_prd_qty() + "],报废数量["
                              + ret_wo.getScrp_prd_qty() + "] + 入库数量 [" + ret_wo.getWh_in_prd_qty() + "] 汇总超标！!");
                      return;
                  }
                  retWODao.update(ret_wo);
              }
          }
    }

    public void errActionFnc(FdpaplyrsO outTrx) {
        outTrx.setRtn_code("0000009");
        outTrx.setRtn_mesg("Invalid Action_flg");
    }
}
